package com.ant.androiddemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ant.androiddemo.constant.Global;
import com.ant.androiddemo.util.ImageUtil;

import java.util.Date;

public class UserActivity extends AppCompatActivity {

    private static final String TAG = Global.LOG_TAG;
    private static final String SPF_TAG = Global.SPF_TAG;

    private TextView tvNickname, tvAccount, tvAge, tvGender, tvCity, tvSchool, tvBirthday, tvSign;
    private ImageView ivAvatar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {
        tvAccount = findViewById(R.id.tv_account_text);
        tvNickname = findViewById(R.id.tv_nickname);
        tvGender = findViewById(R.id.tv_gender);
        tvAge = findViewById(R.id.tv_age);

        tvSchool = findViewById(R.id.tv_school_text);
        tvSign = findViewById(R.id.tv_sign_text);
        tvBirthday = findViewById(R.id.tv_birthday_text);
        tvCity = findViewById(R.id.tv_city);
        ivAvatar = findViewById(R.id.iv_avatar);
    }

    private void initData() {
        SharedPreferences spf = getSharedPreferences(SPF_TAG, MODE_PRIVATE);
        String account = spf.getString("account", "");
        String nickname = spf.getString("nickname", "");
        String gender = spf.getString("gender", "");
        String birthday = spf.getString("birthday", "");
        String city = spf.getString("city", "");

        String school = spf.getString("school", "");
        String sign = spf.getString("sign", "");
        String image64 = spf.getString("image64", "");

        String age = getAgeByBirthDay(birthday);

        tvAge.setText(age + '岁');
        tvAccount.setText(account);
        tvNickname.setText(nickname);
        tvGender.setText(gender);
        tvBirthday.setText(birthday);
        tvCity.setText(city);
        tvSchool.setText(school);
        tvSign.setText(sign);

        ivAvatar.setImageBitmap(ImageUtil.base64ToImage(image64));

    }

    private String getAgeByBirthDay(String birthday) {
        if (TextUtils.isEmpty(birthday)) {
            return "";
        }
        try {
            int index = birthday.indexOf("年");
            String result = birthday.substring(0, index);
            int parseInt = Integer.parseInt(result);
            return String.valueOf(2023 - parseInt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public void toEdit(View view) {
        Intent intent = new Intent(this, EditPersonalActivity.class);
        startActivity(intent);
    }

    public void logout(View view) {
        SharedPreferences spf = getSharedPreferences(SPF_TAG, MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putBoolean("isLogin", false);
        editor.apply();

        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        this.finish();
    }
}